<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="会员ID" prop="memberId">
      <el-input v-model="dataForm.memberId" placeholder="会员ID"></el-input>
    </el-form-item>
    <el-form-item label="账户余额" prop="balance">
      <el-input v-model="dataForm.balance" placeholder="账户余额"></el-input>
    </el-form-item>
    <el-form-item label="冻结账户余额" prop="freezeBalance">
      <el-input v-model="dataForm.freezeBalance" placeholder="冻结账户余额"></el-input>
    </el-form-item>
    <el-form-item label="状态：1正常，2锁定，3删除" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态：1正常，2锁定，3删除"></el-input>
    </el-form-item>
    <el-form-item label="" prop="creator">
      <el-input v-model="dataForm.creator" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updater">
      <el-input v-model="dataForm.updater" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="账户类型：1：现金；2：优果；6：红果" prop="balanceType">
      <el-input v-model="dataForm.balanceType" placeholder="账户类型：1：现金；2：优果；6：红果"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          memberId: '',
          balance: '',
          freezeBalance: '',
          status: '',
          creator: '',
          createTime: '',
          updater: '',
          updateTime: '',
          balanceType: ''
        },
        dataRule: {
          memberId: [
            { required: true, message: '会员ID不能为空', trigger: 'blur' }
          ],
          balance: [
            { required: true, message: '账户余额不能为空', trigger: 'blur' }
          ],
          freezeBalance: [
            { required: true, message: '冻结账户余额不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态：1正常，2锁定，3删除不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updater: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          balanceType: [
            { required: true, message: '账户类型：1：现金；2：优果；6：红果不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/disk/memberbalance/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberId = data.memberBalance.memberId
                this.dataForm.balance = data.memberBalance.balance
                this.dataForm.freezeBalance = data.memberBalance.freezeBalance
                this.dataForm.status = data.memberBalance.status
                this.dataForm.creator = data.memberBalance.creator
                this.dataForm.createTime = data.memberBalance.createTime
                this.dataForm.updater = data.memberBalance.updater
                this.dataForm.updateTime = data.memberBalance.updateTime
                this.dataForm.balanceType = data.memberBalance.balanceType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/disk/memberbalance/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'memberId': this.dataForm.memberId,
                'balance': this.dataForm.balance,
                'freezeBalance': this.dataForm.freezeBalance,
                'status': this.dataForm.status,
                'creator': this.dataForm.creator,
                'createTime': this.dataForm.createTime,
                'updater': this.dataForm.updater,
                'updateTime': this.dataForm.updateTime,
                'balanceType': this.dataForm.balanceType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
